package com.gimi.cloud.jimi.client.controller;

import com.gimi.cloud.jimi.client.service.home.AuthenticationStepService;
import com.gimi.cloud.jimi.client.service.home.HomePageService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.ColumnProdDTO;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import com.gimi.cloud.jimi.dto.user.UserDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/12/27.
 */
@RestController
@RequestMapping("/homePage")
public class HomePageController {
    private final HomePageService homePageService;
    private final AuthenticationStepService authenticationStepService;
    private static Logger logger = LoggerFactory.getLogger(HomePageController.class);

    public HomePageController(HomePageService homePageService,AuthenticationStepService authenticationStepService){
        this.homePageService = homePageService;
        this.authenticationStepService = authenticationStepService;
    }

    @RequestMapping(value = "/columnList")
    public void columnList(@RequestBody BaseDTO params, HttpServletResponse response) {
        long start = System.currentTimeMillis();
        Util.sendResponse(homePageService.homePageColumnList(), response);
        logger.info(String.format("===>耗时 %d ms",System.currentTimeMillis() - start));
    }

    @RequestMapping(value = "/majorAdList")
    public void majorAdList(@RequestBody BaseDTO params, HttpServletResponse response) {
        long start = System.currentTimeMillis();
        Util.sendResponse(homePageService.homePageMajorAdList(), response);
        logger.info(String.format("===>耗时%d",System.currentTimeMillis() - start));
    }

    @RequestMapping(value = "/columnProdList")
    public void columnProdList(@RequestBody ColumnProdDTO dto, HttpServletResponse response) {
    	long start = System.currentTimeMillis();
    	Response resp = new Response();
    	//PageInfo<ColumnProdDTO> pageList = homePageService.columnProdList(dto);
    	String pageList = homePageService.columnProdListFromRedis(dto);
    	if(!StringUtil.isBlank(pageList)){
            Util.sendResponse(pageList,response);
        }else{
    	    logger.info("该栏目没有配置有效的商品列表");
            resp.setRes_data("");
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
            Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
        }
    	logger.info(String.format("===>耗时%d",System.currentTimeMillis() - start));
    }
    
    //获取认证步骤********************************
    @RequestMapping(value = "/getAuthenticationStatus")
    public void getAuthenticationStatus(@RequestBody UserDTO DTO, HttpServletResponse response) {
        Integer page = authenticationStepService.getAuthenticationStatus(DTO.getUserId());
        Response resp = new Response();
        Map<String,Object> map = new HashMap<>();
        map.put("page", page);
        resp.setRes_data(map);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    @RequestMapping(value = "/getAuthenticationInfo")
    public void getAuthenticationInfo(@RequestBody UserDTO DTO, HttpServletResponse response) {
    	int page =  DTO.getPage();
    	Map<String, Object> map = authenticationStepService.getAuthenticationInfo(page, DTO.getUserId());
    	Response resp = new Response();
    	resp.setRes_data(map);
    	resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
    	Util.sendResponse(JsonUtil.toJson(resp), response);
    }

}
